package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.TongZhiBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.serialization.SoapObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class NewTongZhiActivtity extends BaseActivity {
    private Dialog mWeiboDialog;
    TextView txt_title,right;
    RelativeLayout layout_back;
    EditText ETTlt,ETMsg;
    String Tlt,Msg,ID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_tong_zhi_activtity);
        txt_title= (TextView) findViewById(R.id.txt_title);
        right=(TextView) findViewById(R.id.right);
        ETTlt=(EditText) findViewById(R.id.Tlt);
        ETMsg=(EditText) findViewById(R.id.Msg);
        layout_back= (RelativeLayout) findViewById(R.id.layout_back);

        right.setText("发布");
        layout_back.setVisibility(View.VISIBLE);
        right.setVisibility(View.VISIBLE);


        if (getIntent().getStringExtra("ID")!=null){
            Tlt=getIntent().getStringExtra("title");
            Msg=getIntent().getStringExtra("content");
            ID=getIntent().getStringExtra("ID");
            ETTlt.setText(Tlt);
            ETMsg.setText(Msg);
            txt_title.setText("编辑通知");
        }else {
            txt_title.setText("新建通知");
        }

    }



    public void onclick(View view){
        switch (view.getId()){
            case R.id.layout_back:
                finish();
                break;
            case R.id.right:
                if (getIntent().getStringExtra("ID")!=null){
                    XiuGai();

                }else {
                    FaBu();
                }

                finish();
                break;
        }
    }

    private void FaBu() {

        if (TextUtils.isEmpty(ETTlt.getText().toString())){
            Toast.makeText(NewTongZhiActivtity.this,"标题不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(ETMsg.getText().toString())){
            Toast.makeText(NewTongZhiActivtity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        mWeiboDialog = WeiboDialogUtils.
                createLoadingDialog(NewTongZhiActivtity.this,
                        "加载中...");
        Map<String,String> map=new HashMap();
        map.put("bt",ETTlt.getText().toString());
        map.put("nr",ETMsg.getText().toString());
        map.put("TIME",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
        Log.i("新建公告：", map.toString());
        WebServiceUtils.callWebService(Constances.webhosturl, "InsertGetGonggao", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                Log.i("通知接口",result.getProperty(0).toString());
                Toast.makeText(NewTongZhiActivtity.this,"发布成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    private void XiuGai() {

        if (TextUtils.isEmpty(ETTlt.getText().toString())){
            Toast.makeText(NewTongZhiActivtity.this,"标题不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(ETMsg.getText().toString())){
            Toast.makeText(NewTongZhiActivtity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        mWeiboDialog = WeiboDialogUtils.
                createLoadingDialog(NewTongZhiActivtity.this,
                        "加载中...");
        Map<String,String> map=new HashMap();
        map.put("id",ID);
        map.put("bt",ETTlt.getText().toString());
        map.put("nr",ETMsg.getText().toString());
        map.put("time",new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())));
        WebServiceUtils.callWebService(Constances.webhosturl, "UpdateGonggao", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                Log.i("通知接口",result.getProperty(0).toString());
                Toast.makeText(NewTongZhiActivtity.this,"编辑成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}